package com.bjc.scheduler.elasticjob.quickstart;

import com.bjc.scheduler.elasticjob.quickstart.job.FileBackupJob;
import com.bjc.scheduler.elasticjob.quickstart.model.FileCustom;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import javax.swing.*;

/**
 * @program: elastic-job-quickstart
 * @description:  启动任务
 * @author: bjc
 * @create: 2021-08-15 20:05
 */
public class JobMain {

    /**
     * zookeeper端口
     */
    private static final int ZOOKEEPER_PORT = 2181;

    /**
     * zookeeper连接地址
     */
    private static final String ZOOKEEPER_CONNECTION_STRING = "localhost:"+ZOOKEEPER_PORT;

    /**
     * 定时任务命名空间
     */
    private static final String JOB_NAMESPACE = "elastic-job-example-java";


    public static void main(String[] args) {
        //制造一些测试数据
        generateTestFiles();
        //配置注册中心
        CoordinatorRegistryCenter registryCenter = setUpRegistryCenter();
        //启动任务
        startJob(registryCenter);

    }

    /**
     * 注册中心配置
     * @return
     */
    private static CoordinatorRegistryCenter setUpRegistryCenter(){
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(ZOOKEEPER_CONNECTION_STRING, JOB_NAMESPACE);
        //减少zk的超时时间
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        //创建注册中心
        ZookeeperRegistryCenter registryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        registryCenter.init();
        return registryCenter;
    }

    /**
     * 配置并启动任务
     * @param registryCenter
     */
    private static void startJob(CoordinatorRegistryCenter registryCenter){
        //创建JobCoreConfiguration
        //String jobName 任务名称, String cron 调度表达式, int shardingTotalCount 作业分片数量
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("files-job", "0/3 * * * * ?", 3).build();
        //创建SimpleJobConfiguration
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, FileBackupJob.class.getCanonicalName());
        //启动任务
        new JobScheduler(registryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build()).init();
    }

    /**
     * 制造一些测试数据,生成测试文件
     */
    private static void generateTestFiles(){
        for (int i = 0; i < 11; i++) {
            FileBackupJob.files.add(new FileCustom(String.valueOf(i+10),"文件"+(i+10),"text","content"+(i+10)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i+20),"文件"+(i+20),"image","content"+(i+20)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i+30),"文件"+(i+30),"radio","content"+(i+30)));
            FileBackupJob.files.add(new FileCustom(String.valueOf(i+40),"文件"+(i+40),"video","content"+(i+40)));
        }
        System.out.println("生产测试数据完成");
    }
}
